package com.xja.dao.impl;

import com.xja.connutil.ConnUtil;
import com.xja.dao.ZhsDao;
import com.xja.entity.Indent;
import com.xja.entity.Room;
import com.xja.entity.User;
import com.xja.entity.pojo.UrByIndent;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ZhsDaolmpl implements ZhsDao {
    //注册功能
    public Integer addUser(User user) throws SQLException{
        Connection conn = ConnUtil.getConn();
        PreparedStatement ps = conn.prepareStatement("INSERT INTO `user`(user_name,user_phone,user_gender,user_card,user_password,user_level) VALUES(?,?,?,?,?,0)");
        ps.setString(1,user.getUserName());
        ps.setString(2,user.getUserPhone());
        ps.setString(3,user.getUserGender());
        ps.setString(4,user.getUserCard());
        ps.setString(5,user.getUserPassWord());
        int i = ps.executeUpdate();
        return i;
    }

    //登录功能
    public List<User> fingAllUser() throws  SQLException{
        ArrayList<User> AllUserlist = new ArrayList<>();
        Connection conn = ConnUtil.getConn();
        PreparedStatement ps = conn.prepareStatement("select * from User");
        ResultSet rs = ps.executeQuery();
        while (rs.next()){
            User user =new User();
            user.setUserId(rs.getInt("user_id"));
            user.setUserName(rs.getString("user_name"));
            user.setUserPhone(rs.getString("user_phone"));
            user.setUserGender(rs.getString("user_gender"));
            user.setUserCard(rs.getString("user_card"));
            user.setUserLevel(rs.getInt("user_level"));
            user.setUserPassWord(rs.getString("user_password"));
            AllUserlist.add(user);
        }
    return AllUserlist;

    }

    //预定功能 根据房间状态查询可住房型
    public List<Room> findRoom() throws SQLException {
        List<Room> list = new ArrayList<>();
        Connection conn = ConnUtil.getConn();
        PreparedStatement ps = conn.prepareStatement("select room_id,room_kind,room_price from room where room_status=1");
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
            Room room = new Room();
            room.setRoomId(rs.getInt("room_id"));
            room.setRoomKind(rs.getString("room_kind"));
            room.setRoomPrice(rs.getInt("room_price"));
            list.add(room);
        }
        return list;
    }
    //根据用户名称查找用户ID
   public Integer findUserid(String name) throws SQLException{
       Connection conn = ConnUtil.getConn();
       PreparedStatement ps = conn.prepareStatement("select user_id from user where user_name=?");
       ps.setString(1,name);
       ResultSet rs = ps.executeQuery();
       rs.next();
       return rs.getInt("user_id");
    }
    //预定之后更改房间状态
    public Integer updateroomstatus(Integer roomid) throws SQLException {
        Connection conn = ConnUtil.getConn();
        conn.setAutoCommit(false);
        PreparedStatement ps = conn.prepareStatement("update room set room_status=? where room_id = ?");
        ps.setInt(1, 0);
        ps.setInt(2, roomid);
        Integer i = ps.executeUpdate();
        conn.commit();
        return i;
    }


    //在订单表中插入预定信息
    public Integer indent(Indent indent)throws SQLException{
        Connection conn = ConnUtil.getConn();
        PreparedStatement ps = conn.prepareStatement("INSERT INTO indent(user_id,room_id,`check-in time`,duration,remark) VALUES(?,?,?,?,?)");
        ps.setInt(1,indent.getUserId());
        ps.setInt(2,indent.getRoomId());
        ps.setDate(3,new java.sql.Date(indent.getCheckTime().getTime()));
        ps.setInt(4,indent.getDuratiom());
        ps.setString(5,indent.getRemark());
        int i = ps.executeUpdate();
        return i;
    }
    //查询用户自己的订单
    public List<Indent> selecIndent(Integer userid)throws SQLException{
        List<Indent> list = new ArrayList<>();
        Connection conn = ConnUtil.getConn();
        PreparedStatement ps = conn.prepareStatement("select * from indent where user_id=?");
        ps.setInt(1,userid);
        ResultSet rs = ps.executeQuery();
        while (rs.next()){
            {Indent indent = new Indent();
            indent.setRemark(rs.getString("remark"));
            indent.setIndentId(rs.getInt("indent_id"));
            indent.setRoomId(rs.getInt("room_id"));
            indent.setUserId(rs.getInt("user_id"));
            indent.setCheckTime(rs.getDate("check-in time"));
            indent.setDuratiom(rs.getInt("duration"));
            list.add(indent);
            }
        }return list;
    }
    //根据名字查订单
    public List<UrByIndent> selecIndentByName(String username)throws SQLException{
        List<UrByIndent> list = new ArrayList<>();
        Connection conn = ConnUtil.getConn();
        PreparedStatement ps = conn.prepareStatement("select i.*,u.user_name,r.*  from indent i,`user` u ,room r where i.user_id=u.user_id and i.room_id = r.room_id and user_name=?");
        ps.setString(1,username);
        ResultSet rs = ps.executeQuery();
        while (rs.next()){
            {Indent indent = new Indent();
            indent.setRemark(rs.getString("remark"));
            indent.setIndentId(rs.getInt("indent_id"));
            indent.setRoomId(rs.getInt("room_id"));
            indent.setUserId(rs.getInt("user_id"));
            indent.setCheckTime(rs.getTime("check-in time"));
            indent.setDuratiom(rs.getInt("duration"));
            User user = new User();
            user.setUserName(rs.getString("user_name"));
            Room room = new Room();
            room.setRoomId(rs.getInt("room_id"));
            room.setRoomKind(rs.getString("room_kind"));
            room.setRoomPrice(rs.getInt("room_price"));
            room.setRoomFloor(rs.getInt("room_floor"));
            room.setRoomName(rs.getString("room_name"));
            UrByIndent urByIndent = new UrByIndent(user, room,indent);
            list.add(urByIndent);
            }
        }return list;
    }
    //修改已预定的房间信息
    public  Integer updateroomstatus1(Integer roomid) throws SQLException{
        Connection conn = ConnUtil.getConn();
        conn.setAutoCommit(false);
        PreparedStatement ps = conn.prepareStatement("update room set room_status=1 where room_id=?");
        ps.setInt(1,roomid);
        Integer i = ps.executeUpdate();
        conn.commit();
        return i;
    }
    //查询用户自己的订单房间Id
    public Integer selecRoomId(Integer userid)throws SQLException{
        Integer I = null;
        Connection conn = ConnUtil.getConn();
        PreparedStatement ps = conn.prepareStatement("select room_id from indent where user_id=?");
        ps.setInt(1,userid);
        ResultSet rs = ps.executeQuery();
        rs.next();
        return rs.getInt("room_id");}
        //判断用户是否只预定了一个房间

    //删除订单
    public Integer deleteIndent(Integer userid) throws SQLException{
        Connection conn = ConnUtil.getConn();
        PreparedStatement ps = conn.prepareStatement("delete  from indent where user_id = ?");
        ps.setInt(1,userid);
        int i1 = ps.executeUpdate();
        return i1;
    }

    }


